<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

// This funstion validates a plain text password with an
// encrpyted password
function func_validate_password($plain, $encrypted) {
	if( util_not_null($plain) && util_not_null($encrypted) ) {
		// split apart the hash / salt
		$stack = explode(':', $encrypted);
		
		if (sizeof($stack) != 2) return false;
		
		if (md5($stack[1] . $plain) == $stack[0] ) {
			return true;
		}
	}
	
	return false;
}

// This function makes a new password from a plaintext password. 
function func_encrypt_password($plain) {
	$password = '';
	
	for ($i=0; $i<10; $i++) {
		$password .= util_create_random();
	}
	
	$salt = substr(md5($password), 0, 2);
	
	$password = md5($salt . $plain) . ':' . $salt;
	
	return $password;
}

//
function func_make_password() {
	$salt = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	$len = strlen($salt);
	$makepass = "";
	mt_srand(10000000*(double)microtime());
	
	for ($i = 0; $i < 8; $i++) {
		$makepass .= $salt[mt_rand(0,$len - 1)];
	}
	
	return $makepass;
}
//
?>